package com.neusoft.neumooc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neusoft.core.common.BaseController;
import com.neusoft.core.common.conf.Params;
import com.neusoft.neumooc.service.ContentTypeService;
import com.neusoft.neumooc.vo.ContentTypeVO;

@Controller
@RequestMapping(value = "/contenttype")
public class ContentTypeController extends BaseController {

    @Autowired
    private ContentTypeService contentTypeService;

    /**
     * 跳转到内容类型列表页面
     */
    @RequestMapping(value = "/list")
    public String list(Model model) {
        try {
            List<ContentTypeVO> types = contentTypeService.getAllContentTypes();
            model.addAttribute("types", types);
        } catch (Exception e) {
            log.error("获取内容类型列表失败", e);
        }
        return "contenttype/list";
    }
    
    /**
     * 跳转到添加内容类型页面
     */
    @RequestMapping(value = "/add")
    public String add() {
        return "contenttype/add";
    }
    
    /**
     * 跳转到编辑内容类型页面
     */
    @RequestMapping(value = "/edit")
    public String edit(String typeId, Model model) {
        log.debug("开始获取内容类型，typeId: " + typeId);
        try {
            ContentTypeVO type = contentTypeService.getContentTypeById(typeId);
            log.debug("获取到的内容类型信息: " + type);
            if (type != null) {
                model.addAttribute("type", type);
            } else {
                log.error("未找到指定的内容类型: " + typeId);
            }
        } catch (Exception e) {
            log.error("获取内容类型失败", e);
        }
        return "contenttype/edit";
    }
    
    /**
     * 添加内容类型
     */
    @ResponseBody
    @RequestMapping(value = "/doAdd")
    public Map<String, Object> doAdd(ContentTypeVO contentType) {
        Map<String, Object> retMap = new HashMap<String, Object>();
        try {
            boolean result = contentTypeService.addContentType(contentType);
            if(result) {
                retMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
                retMap.put(Params.RET_OBJ, "添加成功");
            } else {
                retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
                retMap.put(Params.RET_OBJ, "添加失败");
            }
        } catch (Exception e) {
            log.error("添加内容类型失败", e);
            retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
            retMap.put(Params.RET_OBJ, "添加内容类型失败");
        }
        return retMap;
    }
    
    /**
     * 更新内容类型
     */
    @ResponseBody
    @RequestMapping(value = "/update")
    public Map<String, Object> update(ContentTypeVO contentType) {
        Map<String, Object> retMap = new HashMap<String, Object>();
        try {
            boolean result = contentTypeService.updateContentType(contentType);
            if(result) {
                retMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
                retMap.put(Params.RET_OBJ, "更新成功");
            } else {
                retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
                retMap.put(Params.RET_OBJ, "更新失败");
            }
        } catch (Exception e) {
            log.error("更新内容类型失败", e);
            retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
            retMap.put(Params.RET_OBJ, "更新内容类型失败");
        }
        return retMap;
    }
    
    /**
     * 删除内容类型
     */
    @ResponseBody
    @RequestMapping(value = "/delete")
    public Map<String, Object> delete(String typeId) {
        Map<String, Object> retMap = new HashMap<String, Object>();
        try {
            boolean result = contentTypeService.deleteContentType(typeId);
            if(result) {
                retMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
                retMap.put(Params.RET_OBJ, "删除成功");
            } else {
                retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
                retMap.put(Params.RET_OBJ, "删除失败");
            }
        } catch (Exception e) {
            log.error("删除内容类型失败", e);
            retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
            retMap.put(Params.RET_OBJ, "删除内容类型失败");
        }
        return retMap;
    }
} 